# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : config.py
# Date      : 2019/7/24
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------

import os
import json
import logging

import configs.config as config

config_dir = os.path.split(__file__)[0]
logger = logging.getLogger(__name__)


def safe_argvs():
    paras = os.listdir(config_dir)
    return [p.split('.')[0] for p in paras if p.split('.')[-1] == 'json']


SAFE_ARGVS = safe_argvs()


def update_config_by_json(module, json_dir):
    with open(json_dir,"r") as f:
        d = json.load(f)
    [setattr(module, k.decode('utf-8'), v) for k, v in d.items()]

#
def update_config_by_name(name="static"):
    if name not in SAFE_ARGVS:
        raise ValueError("safe argvs error {} {}:\n".format(name, SAFE_ARGVS) + " ".join(SAFE_ARGVS))
    json_dir = os.path.join(config_dir , name + ".json")
    update_config_by_json(config, json_dir)
    str_dict_keys = [k for k in dir(config) if k[0].isupper()]
    str_dict = {k: config.__dict__[k] for k in str_dict_keys}
    strs = ["=>{}:{}".format(k, v) for k, v in str_dict.items()]
    strs_info = "\n" + json_dir + ":\n    " + "\n    ".join(strs)
    return strs_info

if __name__ == '__main__':
    print update_config_by_name("global_constant")
    print config.author